﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgencyEntityLib.BaseEntity;
using AgencyEntityLib.Context;

public partial class Ctl_ctlContactFace : System.Web.UI.UserControl
{
    
    wsTask ws_Task = (wsTask)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Task];

    private Func<bool> _OnButtonClick;
    private Func<bool> _OnMyDataChanged;
    private Func<bool> _OnBeforeMyDataDataChanged;
    /// <summary>
    /// События - Кнопка Save|Cancel
    /// </summary>
    public Func<bool> OnButtonClick { get { return _OnButtonClick; } set { _OnButtonClick = value; } }
    public Func<bool> OnMyDataChanged { get { return _OnMyDataChanged; } set { _OnMyDataChanged = value; } }
    public Func<bool> OnBeforeMyDataDataChanged { get { return _OnBeforeMyDataDataChanged; } set { _OnBeforeMyDataDataChanged = value; } }

    public string FaceID { get { return hfContactFaceId.Value; } set { hfContactFaceId.Value = value; } }


    protected ContactFace ContactFace
    {
        get
        {
            wsFace _ws_Face = (wsFace)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Face];
            return _ws_Face.GetContactFace(FaceID);
        }
    }

     protected override void OnDataBinding(EventArgs e)
     {
             base.OnDataBinding(e);
             lvContactItems.DataSource = ContactItems(ContactFace);
             lvContactItems.DataBind();

     }


    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ItemCommand(object sender, ListViewCommandEventArgs e)
    {

    }
    protected void ItemEditing(object sender, ListViewEditEventArgs e)
    {

    }
    protected void btnRemoveContactItem_Click(object sender, ImageClickEventArgs e)
    {                
        ContactFace.ContactList.Remove(((ImageButton)sender).CommandArgument);
        lvContactItems.DataSource = ContactItems(ContactFace);
        lvContactItems.DataBind();        

        
    }
    protected void btnAddContactItem_Click(object sender, ImageClickEventArgs e)
    {
        Contact NewC = new Contact();
        NewC.ContactValue = txtContactValue.Text  ;
        NewC.ContactType = rbContactType.SelectedItem.Value;
        NewC.MainContact = chMainContact.Checked;        

        ContactFace.ContactList.Add(NewC);
        lvContactItems.DataSource = ContactItems(ContactFace);
        lvContactItems.DataBind();
        txtContactValue.Text = string.Empty;        

    }

    protected Contact[] ContactItems(ContactFace Worker)
    {
        Contact[] c = Worker.ContactList.GetSortedList();
        return c;
    }

    protected void btnSaveWorker_Click(object sender, EventArgs e)
    {
        ContactFace.FirstName = txtFirstName.Text;
        ContactFace.SecondName = txtSecondName.Text;
        ContactFace.LastName = txtLastName.Text ;
        ContactFace.Position = txtPosition.Text;
        if (null!=_OnBeforeMyDataDataChanged) OnBeforeMyDataDataChanged();
        wsFace ws = (wsFace)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Face];
        ws.SaveFace(ContactFace);
        if (null != _OnMyDataChanged) OnMyDataChanged(); 

    }

    protected string ClientName 
    { 
        get 
        {
            wsFace ws = (wsFace)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Face];
            Client cl= ws.GetClient (ContactFace .ClientID );
            return cl.FullName;
        } 
    }
}
